package cn.wolfcode.wolf2w.web.controller;

import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.RegionQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.uitl.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("region")
public class RegionController {
    @Autowired
    private IRegionService regionService;
    @Autowired
    private IDestinationService destinationService;
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") RegionQuery qo){
       Page<Region> page =regionService.query(qo);
       model.addAttribute("page",page);
        //直接返回页面
        return "region/list";
    }

    /**
     * 通过区域id查询目的地
     * @par
     * @return
     */
    @RequestMapping("/getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(String rid){
        //在这里是要查询那个就要用到那个的service接口查询
        return destinationService.getDestByRegionId(rid);
    }
    /**
     * 通过区域id修改状态
     * @par
     * @return
     */
    @RequestMapping("/changeHotValue")
    @ResponseBody
    public Object changeHotValue(int hot,String id){
        //下面方法最好是放在业务层处理最好
        if (StringUtils.hasLength(id)){
            //查询
            Region region = regionService.get(id);
            //替换
            region.setIshot(hot);
            //修改
            regionService.update(region);
        }
        return JsonResult.success();
    }
    /**
     * 通过区域id删除数据
     * @par
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        regionService.delete(id);
        return JsonResult.success();
    }
    @RequestMapping("/listDests")
    @ResponseBody
    public Object listDests(){
        //直接返回页面
        return destinationService.lists();
    }
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOUpdate(Region region){
        //NoNullStringUitl.getNull(region.getSequence(),"");
        //直接返回页面
        if (StringUtils.hasLength(region.getId())){
            //1先查询  2再替换  3修改
            //这个是特殊的类,
            regionService.update(region);
        }else {
          regionService.save(region);
        }
       return JsonResult.success();
    }
}
